/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bashe.business.pipeline;

import han.bashe.common.message.NormalizedMessage;
import han.bashe.common.schema.MappingServiceInfoWrapper;
import han.bashe.common.schema.ServiceInfoWrapper;

import java.util.HashMap;

import v1.query.web.bifang.han.WebServicesMethodDocument.WebServicesMethod;

/**
 * @Author Kent
 */
public class PipelineContext {

	private HashMap<String, Object> context = new HashMap<String, Object>();

	private static final String ERROR = "error";

	private static final String SERVICE = "service";

	private static final String RETRY_LOG_ID = "retryLogId";

	private static final String MAPPING_SERVICE = "mappingService";

	private static final String REQUEST = "request";

	private static final String RESPONSE = "response";

	private static final String REQUEST_TIME = "requestTime";

	private static final String RESPONSE_TIME = "responseTime";

	private static final String LOG_REQUEST_DONE = "logRequestDone";

	private static final String BINDING_TO_THIS_BUS = "bindingToThisBus";

	private static final String METHOD = "method";

	private static final String METHOD_QOS = "methodQos";

	private static final String ORGIN_MSG_TYPE = "orginMsgType";

	private static final String RETRY_RECORD_ID = "retryRecordId";

	public void setRetryRecordId(String id) {
		context.put(RETRY_RECORD_ID, id);
	}

	public String getRetryRecordId() {
		return (String)context.get(RETRY_RECORD_ID);
	}

	public void setOrginMsgType(String type) {
		context.put(ORGIN_MSG_TYPE, type);
	}

	public String getOrginMsgType() {
		return (String) context.get(ORGIN_MSG_TYPE);
	}

	public void put(String name, Object value) {
		context.put(name, value);
	}

	public Object get(String name) {
		return context.get(name);
	}

	public void remove(String name) {
		context.remove(name);
	}

	public void clear() {
		context.clear();
	}

	public boolean hasError() {
		return context.get(ERROR) != null;
	}

	public void setError(Exception e) {
		context.put(ERROR, e);
	}

	public Exception getError() {
		return (Exception) context.get(ERROR);
	}

	public ServiceInfoWrapper getService() {
		return (ServiceInfoWrapper) context.get(SERVICE);

	}

	public void setService(ServiceInfoWrapper ser) {
		context.put(SERVICE, ser);
	}

	public void setMappingService(MappingServiceInfoWrapper ser) {
		context.put(MAPPING_SERVICE, ser);
	}

	public MappingServiceInfoWrapper getMappingService() {
		return (MappingServiceInfoWrapper) context.get(MAPPING_SERVICE);
	}

	public boolean isLocalDomainService() {
		return context.get(SERVICE) != null;
	}

	public NormalizedMessage getRequest() {
		return (NormalizedMessage) context.get(REQUEST);
	}

	public void setRequest(NormalizedMessage msg) {
		context.put(REQUEST, msg);
	}

	public NormalizedMessage getResponse() {
		return (NormalizedMessage) context.get(RESPONSE);
	}

	public void setResponset(NormalizedMessage msg) {
		context.put(RESPONSE, msg);
	}

	public boolean isRetry() {
		if (context.get(RETRY_LOG_ID) == null)
			return false;
		else
			return true;
	}

	public void setRetryLogId(String logId) {
		context.put(RETRY_LOG_ID, logId);
	}

	public String getRetryLogId() {
		return (String) context.get(RETRY_LOG_ID);
	}

	public Long getRequestTime() {
		
		return (Long) context.get(REQUEST_TIME);
	}

	public void setRequestTime(Long requestTime) {
		context.put(REQUEST_TIME, requestTime);
	}

	public Long getResponseTime() {
		return (Long)context.get(RESPONSE_TIME);
	}

	public void setResponseTime(Long responseTime) {
		context.put(RESPONSE_TIME, responseTime);
	}

	public boolean isLogRequestDone() {
		if (context.get(LOG_REQUEST_DONE) == null) {
			return false;
		}
		return (Boolean) context.get(LOG_REQUEST_DONE);
	}

	public void setLogRequestDone(Boolean done) {
		context.put(LOG_REQUEST_DONE, done);
	}

	public boolean isBindingToThisBus() {
		if (context.get(BINDING_TO_THIS_BUS) == null)
			return false;
		return (Boolean) context.get(BINDING_TO_THIS_BUS);
	}

	public void setBindingToThisBus(Boolean b) {
		context.put(BINDING_TO_THIS_BUS, b);
	}

	public void setMethod(WebServicesMethod m) {
		context.put(METHOD, m);
	}

	public WebServicesMethod getMethod() {
		return (WebServicesMethod) context.get(METHOD);
	}

	public HashMap<String, String> getMethodQos() {
		return (HashMap<String, String>) context.get(METHOD_QOS);
	}

	public void setMethodQos(HashMap<String, String> qos) {
		context.put(METHOD_QOS, qos);
	}

}
